#include <algorithm>
#include <cstdio>

typedef long long ll;
const int N = 5e4 + 5;
int n, t;
ll sum[N];

struct Node {
  int a, b;
  inline bool operator<(const Node &rhs) const {
    return (a >= b) == (rhs.a >= rhs.b) ? (a < b ? a < rhs.a : b > rhs.b)
                                        : (a >= b) < (rhs.a >= rhs.b);
  }
} x[N];

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("candy.in", "r", stdin);
  freopen("candy.out", "w", stdout);
#endif
#endif

  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) { scanf("%d%d", &x[i].a, &x[i].b); }
    std::sort(x + 1, x + n + 1);
    ll c = x[1].a + x[1].b;
    sum[1] = x[1].a;
    for (int i = 2; i <= n; ++i) {
      sum[i] = sum[i - 1] + x[i].a;
      c = std::max(c, sum[i]) + x[i].b;
    }
    printf("%lld\n", c);
  }
  return 0;
}